import derelict.sdl.sdl;

void putPixel(SDL_Surface *surface, int x, int y, uint pixel)
{
    int bpp = surface.format.BytesPerPixel;
    /* Here p is the address to the pixel we want to set */
    Uint8 *p = cast(Uint8 *)surface.pixels + y * surface.pitch + x * bpp;

    switch(bpp) {
    case 1:
        *p = pixel;
        break;

    case 2:
        *cast(Uint16 *)p = pixel;
        break;

    case 3:
        if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
            p[0] = (pixel >> 16) & 0xff;
            p[1] = (pixel >> 8) & 0xff;
            p[2] = pixel & 0xff;
        } else {
            p[0] = pixel & 0xff;
            p[1] = (pixel >> 8) & 0xff;
            p[2] = (pixel >> 16) & 0xff;
        }
        break;

    case 4:
        *cast(Uint32 *)p = pixel;
        break;
    }
}

void drawPixel(SDL_Surface *surface, int x, int y, uint color){
	int bpp = surface.format.BytesPerPixel;
    Uint8 *  bufp= cast(Uint8 *)surface.pixels + y*surface.pitch + x*bpp;

    switch (bpp) {
    	case 4:
        	bufp[3] = color >> 24;
        case 3:
        	bufp[2] = color >> 16;
        case 2:
        	bufp[1] = color >> 8;
        case 1:
        	bufp[0] = color;
    }
}
